(function (){
	window.App = {
		Models: {},
		Views: {},
		Helpers: {
			getMessage: function (message){
				return Messages[message] 
					? Messages[message] 
					: message;
			}
		}
	}

	//base models
	App.Models.Base = {};

	//main players
	App.Models.Base.GroupOfPeople = Backbone.Model.extend({
		defaults: {
			count: 0,
			power: 0,
			title: "%groupOfPeople%",
			health: 100,
			morale: 50,
			trust: 10,
			//can be "f" (friend), "e" (enemy), "n" (neutral)
			side: "n",
			organization: 50
		}
	});

	App.Models.Base.Building = Backbone.Model.extend({
		defaults: {
			important: 0,
			title: "%Building%",
			health: 100,
			//how many people can accommodate
			capacity: 0
		}
	});

	App.Models.Base.Person = Backbone.Model.extend({
		defaults: {
			important: 0,
			title: "%Person%",
			health: 100,
			//can be "f" (friend), "e" (enemy), "n" (neutral)
			side: "n"
			
		}
	});

	App.Models.Base.Event = Backbone.Model.extend({
		defaults: {			
			title: "%Event%",
			time: 100,
			action: function (){}			
		}
	});

	App.Models.Base.Weapon = Backbone.Model.extend({
		defaults: {			
			title: "%Weapon%",
			power: 0,
			count: 0
		}
	});

	App.Models.Gun = App.Models.Base.Weapon.extend({
		defaults: {			
			title: "%Gun%",
			power: 70,
			count: 0
		}
	});

	App.Models.Bit = App.Models.Base.Weapon.extend({
		defaults: {			
			title: "%Bit%",
			power: 30,
			count: 0
		}
	});

	App.Models.Cops = App.Models.Base.GroupOfPeople.extend({
		defaults: {
			count: 2000,
			power: 70,
			region: "%regionName%",
			title: "%Cops%",
			health: 100,
			morale: 50,
			trust: 70,
			side: "e"
		}
	});

	App.Models.Titushki = App.Models.Base.GroupOfPeople.extend({
		defaults: {
			count: 1000,
			power: 40,
			region: "%regionName%",
			title: "%Titushki%",
			health: 70,
			morale: 60,
			trust: 60,
			side: "e"
		}
	});

	App.Models.PS = App.Models.Base.GroupOfPeople.extend({
		defaults: {
			count: 500,
			power: 50,
			region: "%regionName%",
			title: "%PraviySector%",
			health: 100,
			morale: 90,
			trust: 70,
			side: "f"
		}
	});

	App.Models.Fans = App.Models.Base.GroupOfPeople.extend({
		defaults: {
			count: 5000,
			power: 40,
			region: "%regionName%",
			title: "%PraviySector%",
			health: 70,
			morale: 90,
			trust: 70,
			side: "f"
		}
	});

	App.Models.User = Backbone.Model.extend({
		defaults: {
			name: '%username%',
			weapon: []
		},

		getWeaponsList: function(){
			var weapons = this.get('weapon');
			for(var i=0, l = weapons.length; i>l;i++){
				//create Backbone model and views, rednder it		
			}
		},

		getFriendsList: function() {
			var fiends = '';
			for(var i in App.Models.length){
				//create Backbone model and views, rednder it		
				console.log(i);
			}
		}
	});

	App.Views.User = Backbone.View.extend({
		tagName: 'div',

		template: _.template( $('#userTemplate').html() ),

		initialize: function() {
			this.render();
		},

		render: function() {
			var templateModel = {
				greeting: App.Helpers.getMessage('greeting'),
				name: this.model.get('name'),
				weapons: App.Helpers.getMessage('weapons'),
				weapons_list: this.model.getWeaponsList()
			}
			this.$el.html( this.template(templateModel) );
		}
	});

	App.Models.GraphicObjects = {};
	
	App.Models.GraphicObjects.Stella =  Backbone.Model.extend({
		defaults: {
			path: "m 257.5,201.25 68.75,3.75 30,-1.25 52.5,7.5 27.5,-51.25 -5,-6.25 12.5,-6.25 30,-5 3.75,10 12.5,-3.75 6.25,3.75 -3.75,7.5 8.75,5 -5,12.5 6.25,11.25 -22.5,40 20,6.25 -12.5,20 -12.5,-5 -6.25,2.5 -26.25,63.75 -30,-13.75 1.25,-21.25 -13.75,-3.75 -1.25,-10 -10,-3.75 7.5,-25 -2.5,-17.5 -27.5,-3.75 -7.5,20 -18.75,-1.25 6.25,-20 -41.25,2.5 -7.5,15 -16.25,-2.5 -8.75,-15 -16.25,1.25 z",
			title: App.Helpers.getMessage('stella'),
			description: App.Helpers.getMessage('stella_description')
		}
	});

	App.Models.GraphicObjects.Grushevskogo =  Backbone.Model.extend({
		defaults: {
			path: "m 538.75,363.75 27.5,-21.25 25,-20 15,7.5 -23.75,23.75 2.5,7.5 51.25,31.25 -11.25,11.25 -28.75,-20 1.25,-5 -5,-3.75 -1.25,3.75 -11.25,-7.5 -6.25,0 -6.25,6.25 -3.75,5 z",
			title: App.Helpers.getMessage('grushevskogo'),
			description: App.Helpers.getMessage('grushevskogo_description')
		}
	});

	App.Models.GraphicObjects.Cabmin =  Backbone.Model.extend({
		defaults: {
			path: "m 532.5,366.25 27.5,22.5 -21.25,46.25 -25,41.25 -38.75,81.25 -8.75,3.75 -12.5,27.5 -17.5,-10 15,-26.25 -1.25,-11.25 5,-12.5 3.75,-8.75 -1.25,-7.5 -6.25,-12.5 -3.75,-3.75 18.75,3.75 7.5,0 6.25,-10 -1.25,-15 15,-13.75 5,-12.5 -3.75,0 2.5,-8.75 -23.75,-6.25 -12.5,-1.25 -2.5,-7.5 25,-23.75 20,-15 z",
			title: App.Helpers.getMessage('сabmin'),
			description: App.Helpers.getMessage('сabmin_description')
		}
	});

	App.Helpers.Raphael = function (){
		var that = this;
		this.init = function(){
			that.Raphael = Raphael("main", 800, 600);
			that.attr = {
				fill: "#333",
				opacity: 0.7,
				stroke: "#666",
				"stroke-width": 1,
				"stroke-linejoin": "round"
			};
		}

		this.DrawObject = function (obj){
			var raphaelObject = that.Raphael.path(obj.get('path')).attr(that.attr);
	
			raphaelObject.color = Raphael.getColor();
			raphaelObject[0].style.cursor = "pointer";
			raphaelObject[0].onclick = function () {
				//user.set('health', 20);
				var dialogDiv = $("#object-modal");
				dialogDiv.html(obj.get('description'));
				dialogDiv.dialog({ title:obj.get('title') });				
				//dialogDiv.dialog();
			};
			raphaelObject[0].onmouseover = function () {
				raphaelObject.animate({fill: raphaelObject.color, stroke: "#ccc",opacity: 0.9}, 500);
				raphaelObject.toFront();
				that.Raphael.safari();

			};
			raphaelObject[0].onmouseout = function () {
				raphaelObject.animate({fill: "#333", stroke: "#666",opacity: 0.7}, 500);
				raphaelObject.toFront();
				that.Raphael.safari();
			};
		}
	}

})();


$(document).ready(function (){
	var user = new App.Models.User({
		name: 'Алекс',
		weapon: [
			new App.Models.Gun({count:10}),
			new App.Models.Bit({count:500})
		]
	});
	var userView = new App.Views.User({ model: user });

	var Fans = new App.Models.Fans({
		count: 500,
		title: "%DynamoFans%"
	});
	//need to link many models and it instances

	$('#user-details').html(userView.el);

	//initialize raphael
	var R = new App.Helpers.Raphael();
	R.init();

	for(var obj in App.Models.GraphicObjects) {
		R.DrawObject(new App.Models.GraphicObjects[obj]());
	}	

	$('#timer').timeTo({
		seconds: 10, 
		callback: function(){ 
			//do all attached events
			//user.set('health', 20);
		},
		fontSize: 14
	});

});
